package smartcampus.scjava.service.impl;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
import smartcampus.scjava.POJO.DO.CsmsUser;
import smartcampus.scjava.mapper.CsmsUserMapper;
import smartcampus.scjava.service.CsmsUserService;
import smartcampus.scjava.utils.GetRandomUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 雨辰YC
 * @date 2023/12/7 10:54
 * @description CsmsUserServiceImpl
 */
@Service
public class CsmsUserServiceImpl implements CsmsUserService {

    @Resource
    CsmsUserMapper csmsUserMapper;

    @Override
    public boolean generateUser(String username) {
        CsmsUser csmsUser = new CsmsUser();
        Integer userID = GetRandomUtils.getRandomInt();
        System.out.println(csmsUserMapper);
        if (getCsmsUserByUserName(username) != null){
            return false;
        }
        while (getCsmsUserByUserID(userID) != null){
            userID = GetRandomUtils.getRandomInt();
        }
        Date date = new Date();
        csmsUser.setUserID(userID);
        csmsUser.setUsername(username);
        csmsUser.setCreateTime(date);
        csmsUser.setUpdateTime(date);
        return csmsUserMapper.generateUser(csmsUser);
    }

    @Override
    public CsmsUser getCsmsUserByUserName(String username) {
        return csmsUserMapper.getCsmsUserByUserName(username);
    }

    @Override
    public CsmsUser getCsmsUserByUserID(Integer UserID) {
        if (csmsUserMapper.getCsmsUserByUserID(UserID) == null){
            return null;
        }
        return csmsUserMapper.getCsmsUserByUserID(UserID);
    }
}
